<?php

namespace App\Console\Commands\Tools;

use App\Kuafu\Pay\BasePay;
use App\Kuafu\Pay\Channel\FuYou\FuYouBase;
use App\Kuafu\Pay\Service\PayService;
use App\Kuafu\Shop\Enum\ShopCacheEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopCameraSyncService;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SyncShopCompanyCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tools:refresh-shop-company';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Refresh The Shop's Company";


    /**
     * Execute the console command
     *
     * @return int
     *
     */
    public function handle()
    {
        Config::set('database.connections.tiger', [
            'driver' => 'mysql',
            'url'    => '',

            'host'     => 'rm-bp1ii2tq3824rw7uzxo.mysql.rds.aliyuncs.com',
            'database' => 'tiger_prod',
            'username' => 'readonly',
            'password' => '4PuCQS5JXBYjc7V2eygf',

//            'host'           => 'rm-bp1066pu30bmyf7t19o.mysql.rds.aliyuncs.com',
//            'database'       => 'kf_tiger',
//            'username'       => 'kf_order',
//            'password'       => 'order@1014',

            'port'           => '3306',
            'unix_socket'    => '',
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_general_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => false,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                \PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]);

        $companyList = DB::connection('tiger')->table('open_shop_organization_manage')->where('isDeleted', 0)->get()
                         ->map(function ($value) {
                             return (array)$value;
                         })->toArray();

        $companyList = array_column($companyList, null, 'id');

        $provinceList = DB::connection('tiger')->table('open_shop_organization_region_mapping')->where('isDeleted', 0)
                          ->get()
                          ->map(function ($value) {
                              return (array)$value;
                          })->toArray();

        $provinceCodeList = array_column($provinceList, null, 'provinceCode');

        $shopList = Shop::query()->where('province', '!=', 0)->get();

        foreach ($shopList as $one) {
            if (empty($provinceCodeList[$one->province])) {
                $this->info($one->name . '门店不存在');
                continue;
            }

            if (empty($companyList[$provinceCodeList[$one->province]['manageId']])) {
                $this->info($provinceCodeList[$one->province]['provinceName'] . '分公司不存在');
                continue;
            }

            $companyName = $companyList[$provinceCodeList[$one->province]['manageId']]['name'];

            if ($one->branch != $provinceCodeList[$one->province]['provinceName']) {
                Shop::query()->where('id', $one->id)
                    ->update(['branch' => $companyName]);

                Cache::forget(ShopCacheEnum::getShopInfo($one->id));
                Cache::forget(ShopCacheEnum::getShopArchivesInfo($one->id));

                $this->info('id:' . $one->id . '更新成功.');
            }
        }

        ShopService::delSelectionCache();

        $this->info('完成');

        SendFs::newRobot(SendFs::ROBOT_BUSINESS_WARING)->setTitleContent('分公司同步完成', '分公司同步完成')->send();
    }
}
